package com.biz.primus.ms.promotion.dao.ro;

import com.biz.primus.base.redis.annotation.Ro;
import com.biz.primus.base.redis.annotation.RoSortedSet;
import com.biz.primus.model.promotion.enums.PromotionStatusEnum;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Objects;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

/**
 * 秒杀活动Ro
 *
 * @author david-liu
 */
@Getter
@Setter
@Ro(key = "pmt:seckill")
@RoSortedSet(key = "list", score = "createTimestamp")
public class SeckillPromotionRo extends BasePromotionRo<Long> {
    private static final long serialVersionUID = 3633422394753504081L;

    /**
     * 秒杀活动日
     */
    private LocalDate promotionDate;

    /**
     * 秒杀活动开始时间点
     */
    private LocalTime startTimePoint;

    /**
     * 秒杀活动结束时间点
     */
    private LocalTime endTimePoint;

    /**
     * 促销活动ID
     */
    private Long seckillPromotionId;

    /**
     * 促销名
     */
    private String name;

    /**
     * 促销介绍
     */
    private String description;

    /**
     * 活动生效的渠道(APP/微商城)
     */
    private String channels;

    /**
     * 活动开始时间
     */
    private LocalDateTime startTime;

    /**
     * 活动结束时间
     */
    private LocalDateTime endTime;

    /**
     * 享受活动的会员等级集合
     */
    private String effectMemberLevels;

    /**
     * 享受活动的会员标签
     */
    private String effectMemberTags;

    /**
     * 启用状态
     */
    private String commonStatus;

    /**
     * 是否能使用优惠券
     */
    private Boolean allowCoupon = false;

    /**
     * 用户是否只能参与一次
     */
    private Boolean userOnceGainLimit = false;

    /**
     * 判断秒杀活动是否已经完成
     */
    public boolean isPromotionFinished() {
        LocalTime now = LocalTime.now();
        LocalDate today = LocalDate.now();
        return !today.equals(this.promotionDate) || now.isBefore(startTimePoint) ||
                now.isAfter(endTimePoint);
    }

    /**
     * 获取秒杀活动的状态
     */
    public PromotionStatusEnum getPromotionStatus(Boolean hasMemberGained) {
        LocalTime now = LocalTime.now();
        LocalDate today = LocalDate.now();
        if (today.isBefore(promotionDate)) {
            return PromotionStatusEnum.NOT_START;
        } else {
            if (promotionDate.equals(today)) {
                if (now.isAfter(startTimePoint) && now.isBefore(endTimePoint)) {
                    return Objects.nonNull(hasMemberGained) && hasMemberGained ? PromotionStatusEnum.MEMBER_HAS_GAINED : PromotionStatusEnum.HAS_STARTED;
                } else {
                    return PromotionStatusEnum.NOT_START;
                }
            } else {
                return PromotionStatusEnum.FINISHED;
            }
        }
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
    }
}
